Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Explore surface syntax for records with optional fields. #588

Merged
merged 13 commits into from
Jun 27, 2022

Conversation

cristianoc
Copy link
Contributor

@cristianoc cristianoc commented Jun 27, 2022

Declaration

type t = { x: int, y?: string}

Access

let foo = (r:t) => switch r.y {
  | None => ""
  | Some(s) => s
}

Construction and update

let r = { x:3, y: "abc" }
let r2 = {...r, y: "def" }
let r3 = {...r, y: ?None }
let r4 = {...r, y: ?r2.y }

Pattern matching

switch r {
  | {y: "abc" } => "It is abc"
  | {y} => "It is something"
  | {y:?None} => "It is nothing"
}

Punning

let make1 = (~x, ~y} => {x; y}
let make2 = (~x, ?y, ()) => {x; ?y}
let isYSome = (r) => switch r {
  | {x:2, ?y} => y != None
  | _ => false
}

Currently uses @ns.optional which the type checker will need to learn about (knows @optional).

@cristianoc cristianoc force-pushed the surface_syntax_optional_fields branch from aeb4640 to f149fd1 Compare June 27, 2022 14:31
@cristianoc cristianoc changed the title Explore surface syntax for optional attributes. Explore surface syntax for records with optional fields. Jun 27, 2022
This was referenced Jun 27, 2022
@mununki
Copy link
Member

mununki commented Jun 27, 2022

Fantastic work! 👍

cristianoc added a commit to rescript-lang/rescript that referenced this pull request Jun 27, 2022
@cristianoc cristianoc merged commit 45693d2 into master Jun 27, 2022
cristianoc added a commit to rescript-lang/rescript that referenced this pull request Jun 27, 2022
mununki pushed a commit to mununki/rescript-compiler that referenced this pull request Jul 15, 2022
@cristianoc cristianoc deleted the surface_syntax_optional_fields branch July 23, 2022 04:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants